package com.zz.rpc;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zz
 * @Date 2022/5/13
 */
public class RpcProxyServer {
    /**
     *
     * @param service   需要发布的服务
     * @param port 暴露的端口
     */

    private final ExecutorService executorService= Executors.newCachedThreadPool();
    public void publisher(Object service,int port) throws IOException {
        ServerSocket serverSocket = null;
        try {
            serverSocket=new ServerSocket(port); //启动一个服务
            while (true){
                final Socket socket=serverSocket.accept();   //获得一个远程连接(默认为阻塞状态)

                //BIO模型
                //socket.getInputStream();  //这里是阻塞的，io阻塞，不可以处理多个连接

                //通过线程池就可以处理多个连接
                executorService.execute(new ProcessorHandler(socket,service));
            }
        }catch (Exception e){

        }finally {
            if(serverSocket!=null){
                serverSocket.close();
            }
        }
    }
}
